package com.hsy.bibc.modules.common;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.deparse.dpframe.ui.activity.DPBaseActivityWithSimpleActionBar;
import com.deparse.dpframe.utils.DPFileUtil;
import com.hsy.bibc.R;
import com.hsy.bibc.utils.CommonUtils;

import java.io.File;


/**
 * Created by hsy on 2016/3/23.
 */
public class CommonBaseActivityWithSimpleActionBar extends DPBaseActivityWithSimpleActionBar {

    private View networkFailureView;
    private View networkRefreshButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int color = getResources().getColor(R.color.color_action_bar_bg);
        setActionBarBackgroundColor(color);
        getRootView().setBackgroundColor(getResources().getColor(R.color.app_background_color));

        setTitleTextAppearance(R.style.style_page_title);
        setLeftButtonIcon(R.mipmap.action_bar_back);

        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.setImageResource(R.color.separator_line);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addViewInHeader(imageView, params);
    }

    private void addNetworkFailureScreen() {
        networkFailureView = LayoutInflater.from(this).inflate(R.layout.http_error_layout, null);
        networkRefreshButton = networkFailureView.findViewById(R.id.network_refresh);
        networkRefreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNetworkRefresh();
            }
        });
        networkFailureView.setVisibility(View.GONE);
        addContentView(networkFailureView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        addNetworkFailureScreen();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        addNetworkFailureScreen();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        addNetworkFailureScreen();
    }

    @Override
    protected void onStop() {
        super.onStop();
        CommonUtils.hideProgressDialog();
        System.gc();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        File cacheDir = new File(DPFileUtil.getCacheDir(this));
        if (cacheDir.exists() && cacheDir.isDirectory()) {
            DPFileUtil.emptyDir(cacheDir);
        }
    }

    public void showNetworkFailureScreen() {
        getRootView().setVisibility(View.GONE);
        networkFailureView.setVisibility(View.VISIBLE);
    }

    public void hideNetworkFailureScreen() {
        getRootView().setVisibility(View.VISIBLE);
        networkFailureView.setVisibility(View.GONE);
    }

    protected void onNetworkRefresh() {

    }
}
